package cn.historia.job;

import cn.historia.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @package: cn.historia.job
 * @className: TimeoutCloseOrderJob
 * @author: 云溪
 * @description: 超时关单
 * @date: 2025/7/9 21:59
 * @version: 1.0
 */

@Slf4j
@Component
public class TimeoutCloseOrderJob {

    @Resource
    private IOrderService orderService;

    @Scheduled(cron = "0 0/10 * * * ?")
    public void exec() {
        try{
            log.info("任务：检测订单超时30分钟未支付，订单关闭");
            List<String> orderIds = orderService.queryTimeoutCloseOrderList();
            if(null == orderIds || orderIds.isEmpty()){
                log.info("定时任务，超时30分钟订单关闭，暂无超时未支付订单 orderIds is null");
                return;
            }
            for(String orderId : orderIds){
                boolean status = orderService.changeOrderClose(orderId);
                log.info("定时任务，超30分钟订单关闭 orderId: {} status: {}", orderId, status);
            }

        } catch(Exception e){
            log.error("定时任务，超时15分钟订单关闭失败", e);
        }
    }

}
